﻿using System;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using WidgetInterface;

/// <summary>
/// Author: AustralTek 
/// Create date: 2010/05/04
/// http://www.australtek.com
/// Copyright: 2009 - 2010
/// Summary description for DBDataSet
/// </summary>

namespace IndustrialDashboard
{
    public class DBDataSet : IWidget
    {
        private String strSource = "DBDataSet";
        public struct FormatedTable
        {
            public List<object> ColumnsNamesList;
            public List<object> Matrix;
        }
        public Dictionary<string, FormatedTable> tables;
        public DBDataSet()
        {
            //
            // TODO: Add constructor logic here
            //           
            Logger.Info(strSource,"Initializing");            
        }
        public DBDataSet(DataSet ds)
        {
            try
            {
                Logger.Info(strSource, "Initializing");                
                tables = GetFormatedDataSet(ds);
            }
            catch (Exception ex) 
            {
                Logger.Warn(strSource, "", ex);
                Logger.Error(strSource, "", ex);
                throw ex;             
            }
        }

        private Dictionary<string, FormatedTable> GetFormatedDataSet(DataSet data)
        {
            return data.Tables.Cast<DataTable>().ToDictionary(t => t.TableName, t => GetFormatedTable(t));
        }
        private FormatedTable GetFormatedTable(DataTable table)
        {
            FormatedTable ft = new FormatedTable();
            try
            {
                ft.ColumnsNamesList = GetColumnNamesList(table);
                ft.Matrix = GetMatrix(table);
                return ft;
            }
            catch (Exception ex) 
            {
                Logger.Warn(strSource, "", ex);
                Logger.Error(strSource, "", ex);
                throw ex;             
            }
        }
        private List<object> GetColumnNamesList(DataTable table)
        {
            return table.Columns.Cast<DataColumn>().Select(c => (object)c.ColumnName).ToList();
        }
        private List<object> GetMatrix(DataTable table)
        {
            List<object> matrix = new List<object>();
            try
            {
                foreach (DataRow row in table.Rows)
                    matrix.AddRange(row.ItemArray.ToList());
                return matrix;
            }
            catch (Exception ex) 
            {
                Logger.Warn(strSource, "", ex);
                Logger.Error(strSource, "", ex);
                throw ex; 
            }
        }

        #region IWidget Members

        public object GenerateResponse(DataSet ds)
        {
            DBDataSet sqlds = new DBDataSet(ds);
            return (Object)sqlds;
        }

        public object GetExcelChunk(System.IO.Stream document)
        {            
            Logger.Warn(strSource, "GetExcelChunk() [NotImplementedException]");
            throw new NotImplementedException();
        }

        public object GetPDFChunk(System.IO.Stream document)
        {
            Logger.Warn(strSource, "GetPDFChunk() [NotImplementedException]");
            throw new NotImplementedException();
        }

        #endregion
    }
}
